package tire

import "fmt"

type TrieNode struct {
	value      int
	dictionary [26]*TrieNode
}

type TrieTree struct {
	root *TrieNode
}

func CreateTree(arrayList []string) TrieTree {
	myTree := TrieTree{}
	//添加跟节点
	myTree.root = &TrieNode{}
	for _, value := range arrayList {
		myTree.AddWord(value)
	}
	return myTree
}
func (t *TrieTree) AddWord(word string) {
	fmt.Println(word)
	nowNode := t.root
	a := int('a')
	var char int
	for i := 0; i < len(word); i++ {
		char = int(word[i])
		if nowNode.dictionary[char-a] != nil {
			nowNode = nowNode.dictionary[char-a]
			continue
		} else {
			newNode := &TrieNode{}
			nowNode.dictionary[char-a] = newNode
			nowNode = newNode
			continue
		}
	}
}

func (t *TrieTree) FindWord(word string) (back int) {
	nowNode := t.root
	a := int('a')
	var char int
	defer func() {
		err := recover()
		if err != nil {
			fmt.Println(err)
			back = 1
		}
	}()
	for i := 0; i < len(word); i++ {
		char = int(word[i])
		if nowNode.dictionary[char-a] != nil {
			return 0
		} else {
			nowNode = nowNode.dictionary[char-a]
		}
		if i == len(word)-1 {
			return 1
		}
	}
	return 0
}
